// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EchoAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Test.Robotics.Runtime
{
    using System.Runtime.Serialization;
    using System.Threading;
    using Microsoft.Robotics.Runtime;
    
    /// <summary>
    /// An agent that implements the publishing and control interfaces twice.
    /// Used to validate that the metadata discovery code correctly find and disambiguates between them.
    /// </summary>
    [DataContract]
    [System.Diagnostics.CodeAnalysis.SuppressMessage(
    "Microsoft.Design",
    "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable",
    Justification = "Agents clean up resources when OnDeactivated() is called")]
    public class EchoAgent : Agent, 
                             IMessagePublisher<SimpleAgentMessage<int>>, 
                             IMessagePublisher<SimpleAgentMessage<double>>, 
                             IControlReceiver<SimpleAgentMessage<int>>, 
                             IControlReceiver<SimpleAgentMessage<double>>
    {
        /// <summary>
        /// The first publisher.
        /// </summary>
        private IForwarder<SimpleAgentMessage<int>> publisher1;

        /// <summary>
        /// The second publisher.
        /// </summary>
        private IForwarder<SimpleAgentMessage<double>> publisher2;

        /// <summary>
        /// True if the first receiver was called.
        /// </summary>
        private bool wasReceive1Called;

        /// <summary>
        /// True if the second receiver was called.
        /// </summary>
        private bool wasReceive2Called;

        /// <summary>
        /// Signals when both Receive methods have been called.
        /// </summary>
        private ManualResetEvent allMethodsCalledEvent;

        /// <summary>
        /// Initializes a new instance of the EchoAgent class.
        /// </summary>
        /// <param name="name">The name of the agent instance.</param>
        public EchoAgent(string name)
            : base(name)
        {
            this.allMethodsCalledEvent = new ManualResetEvent(false);
        }

        /// <summary>
        /// Waits until the agent receives at least one message of each type or until the timeout period lapsed
        /// </summary>
        /// <param name="timeout">The amount of time to wait before giving up, in ms</param>
        /// <returns>True if the agent received at least one message of each type before the timeout expired, false otherwise</returns>
        public bool Wait(int timeout)
        {
            return this.allMethodsCalledEvent.WaitOne(timeout);
        }

        /// <summary>
        /// Called to pass in the publishing forwarder.
        /// </summary>
        /// <param name="forwarder">The forwarder to use for messages of this type.</param>
        public void InitializePublisher(IForwarder<SimpleAgentMessage<int>> forwarder)
        {
            this.publisher1 = forwarder;
        }

        /// <summary>
        /// Called to pass in the publishing forwarder.
        /// </summary>
        /// <param name="forwarder">The forwarder to use for messages of this type.</param>
        public void InitializePublisher(IForwarder<SimpleAgentMessage<double>> forwarder)
        {
            this.publisher2 = forwarder;
        }

        /// <summary>
        /// Called to process control messages of this type.
        /// </summary>
        /// <param name="message">Control message</param>
        public void ReceiveControl(SimpleAgentMessage<int> message)
        {
            this.wasReceive1Called = true;
            this.publisher1.Post(message);
            if (this.wasReceive2Called)
            {
                this.allMethodsCalledEvent.Set();
            }
        }

        /// <summary>
        /// Called to process control messages of this type.
        /// </summary>
        /// <param name="message">Control message</param>
        public void ReceiveControl(SimpleAgentMessage<double> message)
        {
            this.wasReceive2Called = true;
            this.publisher2.Post(message);
            if (this.wasReceive1Called)
            {
                this.allMethodsCalledEvent.Set();
            }
        }
    }
}
